package cn.wolfcode.luowowo.common.utils;

import cn.wolfcode.luowowo.common.exception.LogicException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AssertUtil {

    public static void hasLength(String s,String msg){
        if(s == null || s.trim().length() == 0){
            throw new LogicException(msg);
        }
    }
    public static void hasObject(Object s,String msg){
        if(s == null ){
            throw new LogicException(msg);
        }
    }

    public static void chekPasswordEquals(String s1,String s2){
        hasLength(s1,"密码或确认密码不能为空");
        hasLength(s2,"密码或确认密码不能为空");
        if(!s1.equals(s2)){
            throw new LogicException("两次密码不匹配");
        }
    }

    public static void chekVerifyCodeEquals(String s1,String s2){
        hasLength(s1,"验证码不能为空");
        hasLength(s2,"验证码超时,请重新获取验证码");
        if(!s1.equals(s2)){
            throw new LogicException("验证码不匹配");
        }
    }
    public static void testPhone(String phone) {
        if(phone.length()<11){
            throw new LogicException("手机号码长度不能少于11位");
        }
        Pattern pat = Pattern.compile("^[1][3578][0-9]{9}$");
        Matcher mat = pat.matcher(phone);
        if(!mat.find()){
            throw new LogicException("手机号码格式不正确");
        }
    }

}
